package pluginGenerator;

import java.util.ArrayList;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IJavaProject;

import ctxSemanticsGen.CtxSemanticsGenerator;
import ctxSemanticsGen.XmlProjectAnalyzer;
import ctxSemanticsGen.appDependent.ClassFetcher;
import ctxSemanticsGen.appDependent.VelocityInitializer;

public class PluginCodeGenerator {
	
	
	//"../TestCOPMethSynt/COPdescriptor.xml";
	private static String file;
	private static String target;
	
	
	
	public static void generateCode(IJavaProject selectedProject)
	                                throws ClassNotFoundException {
		
		/* Get the workspace location */
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		IPath path = root.getLocation();
		String  wsPath = path.toFile().getPath();
		
		/* Get the project location */
		String prjPath = wsPath + selectedProject.getPath().toString();
		
		/* Now build the paths to conf file and generation output folder */
		file = prjPath + "/" + "COPdescriptor.xml"; // TODO: make portable
		target = prjPath + "/src/machinery"; // TODO: hardcoded, change this
		
		
		
		// Get the layered classes and the layers in the project
		XmlProjectAnalyzer xpa = new XmlProjectAnalyzer(file);
		ArrayList<String> classesToProcess = xpa.getLayeredClasses();
		ArrayList<String> layersInSystem = xpa.getLayers();
	
		// Generate COP code in the specified location 
		ClassFetcher cf = new PluginClassFetcher(selectedProject);
		VelocityInitializer rl = new PluginVelocityInitializer();
		CtxSemanticsGenerator csg;
		csg = new CtxSemanticsGenerator(target,classesToProcess,
				                        layersInSystem,cf,rl);
		csg.run();
	}


}
